package com.dylan.springdatajpa.persistence.entity.common;

import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;

import java.util.List;
import java.util.Objects;

/**
 * @author zhangshaojian
 * @description 分页封装
 * @date 2022/2/11 15:59
 */
@Data
public class PageData<T> {

    private int number;
    private int size;
    private int totalPages;
    private long totalElements;
    private List<T> content;

    {
        number = 1;
        size = 20;
    }

    public static <T> PageData<T> of(Page<T> page) {
        PageData<T> pageData = new PageData<>();
        if (Objects.nonNull(page)) {
            BeanUtils.copyProperties(page, pageData);
        }
        return pageData;
    }
}
